import cgi
from google.appengine.ext import db, webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class UserProfile(db.Model):
    name = db.StringProperty(default='Unknown', required=True)
    shoppingScale = db.IntegerProperty(default=0, required=True)
    historyScale = db.IntegerProperty(default=0, required=True)
    ecologyScale = db.IntegerProperty(default=0, required=True)
    gastronomyScale = db.IntegerProperty(default=0, required=True)
    cultureScale = db.IntegerProperty(default=0, required=True)
    nightLifeScale = db.IntegerProperty(default=0, required=True)


class UserProfilePage(webapp.RequestHandler):
    def get(self):
        self.response.out.write("<html><body>")

        #User creation
        self.response.out.write("<p><b><font size=\"5\">Create a user</font></b></p>")
        self.response.out.write("""
                    <form action="/user/createUser" method= "post">
                      <p> <b> Name*:</b><br>
                      <input type="text" name="name" size="50" maxlenght="50"> </p>
                      <p> <b> User interests*: </b> <br>
                      <table border="0" width="300" id="table1">
                      <tr>
                      <td align="right"> Shopping interest*: </td>
                      <td> <select name="shoppingScale" size="1"> <option value="">Chose one</option> <option value="1">Very low</option> <option value="3">Low</option> <option value="5">Medium</option> <option value="7">High</option> <option value="10">Very high</option> </select> </td>
                      </tr><tr>
                      <td align="right"> History interest*: </td>
                      <td> <select name="historyScale" size="1"> <option value="">Chose one</option> <option value="1">Very low</option> <option value="3">Low</option> <option value="5">Medium</option> <option value="7">High</option> <option value="10">Very high</option> </select> </td>
                      </tr><tr>
                      <td align="right"> Ecology interest*: </td>
                      <td> <select name="ecologyScale" size="1"> <option value="">Chose one</option> <option value="1">Very low</option> <option value="3">Low</option> <option value="5">Medium</option> <option value="7">High</option> <option value="10">Very high</option> </select> </td>
                      </tr><tr>
                      <td align="right"> Gastronomy interest*: </td>
                      <td> <select name="gastronomyScale" size="1"> <option value="">Chose one</option> <option value="1">Very low</option> <option value="3">Low</option> <option value="5">Medium</option> <option value="7">High</option> <option value="10">Very high</option> </select> </td>
                      </tr><tr>
                      <td align="right"> Culture interest*: </td>
                      <td> <select name="cultureScale" size="1"> <option value="">Chose one</option> <option value="1">Very low</option> <option value="3">Low</option> <option value="5">Medium</option> <option value="7">High</option> <option value="10">Very high</option> </select> </td>
                      </tr><tr>
                      <td align="right"> Night life interest*: </td>
                      <td> <select name="nightLifeScale" size="1"> <option value="">Chose one</option> <option value="1">Very low</option> <option value="3">Low</option> <option value="5">Medium</option> <option value="7">High</option> <option value="10">Very high</option> </select> </td>
                      </tr>
                      </table>
                      </p>
                      <p> <input type="submit" value="Create"> </p>
                    </form>
                    """)



        #User deletion
        usersProfs = db.GqlQuery("SELECT * FROM UserProfile ORDER BY name ASC")
        self.response.out.write("<br><br> <p><b><font size=\"5\">Delete a user</font></b>")
        self.response.out.write("<form action=\"/user/deleteUser\" method= \"post\">")
        self.response.out.write("<select name=\"userProfCombo\" size=\"1\">")
        self.response.out.write("<option value=\"\">Choose one</option>")
        
        for user in usersProfs:
            self.response.out.write("<option value=\"")
            self.response.out.write(user.key())
            self.response.out.write("\">")
            self.response.out.write(user.name)
            self.response.out.write("</option>")

        self.response.out.write("<input type=\"submit\" value=\"Delete\">")
        self.response.out.write("</form> </p>")
        

        #User details
        self.response.out.write("<br><br> <p><b><font size=\"5\">Get user details</font></b>")
        self.response.out.write("<form action=\"/user/getUser\" target=\"_blank\" method= \"post\">")
        self.response.out.write("<select name=\"userList\" size=\"1\">")
        self.response.out.write("<option value=\"\">Choose one</option>")
        
        for user in usersProfs:
            self.response.out.write("<option value=\"")
            self.response.out.write(user.key())
            self.response.out.write("\">")
            self.response.out.write(user.name)
            self.response.out.write("</option>")

        self.response.out.write("<input type=\"submit\" value=\"Get\">")
        self.response.out.write("</form> </p>")
        
        self.response.out.write("</body></html>")


class UserCreation(webapp.RequestHandler):
    def post(self):
        userProf = UserProfile()
        userProf.name = cgi.escape(self.request.get('name'))
        userProf.shoppingScale = int(self.request.get('shoppingScale'))
        userProf.historyScale = int(self.request.get('historyScale'))
        userProf.ecologyScale = int(self.request.get('ecologyScale'))
        userProf.gastronomyScale = int(self.request.get('gastronomyScale'))
        userProf.cultureScale = int(self.request.get('cultureScale'))
        userProf.nightLifeScale = int(self.request.get('nightLifeScale'))
        
        userProf.put()
        self.redirect('/user')

class UserDeletion(webapp.RequestHandler):
    def post(self):
        entity = db.get(self.request.get('userProfCombo'))
        entity.delete()
        self.redirect('/user')

class UserDetails(webapp.RequestHandler):
    def post(self):
        user = db.get(self.request.get('userList'))
        self.response.out.write("""
            <html> <body>
            <table border="0" width="80%" id="table">

            <tr>
            <td align="right"> <b> Name: &nbsp; </b> </td>
            <td> """)
        self.response.out.write(user.name)
        self.response.out.write("""
            </td>
            </tr>

            <tr>
            <td align="right" valign="top"> <b> Profile: &nbsp; </b> </td>
            <td>
            Shopping interest: """)
        self.response.out.write(user.shoppingScale)
        self.response.out.write("<br> History interest: ")
        self.response.out.write(user.historyScale)
        self.response.out.write("<br> Ecology interest: ")
        self.response.out.write(user.ecologyScale)
        self.response.out.write("<br> Gastronomy interest: ")
        self.response.out.write(user.gastronomyScale)
        self.response.out.write("<br> Culture interest: ")
        self.response.out.write(user.cultureScale)
        self.response.out.write("<br> Night life interest: ")
        self.response.out.write(user.nightLifeScale)
        self.response.out.write("</td> </tr> </table>")
        


application = webapp.WSGIApplication([('/user', UserProfilePage), ('/user/createUser', UserCreation), ('/user/deleteUser', UserDeletion), ('/user/getUser', UserDetails)], debug=True)


def main():
  run_wsgi_app(application)

if __name__ == '__main__':
  main()


        
